import java.util.Stack;


public class MotorCautare {
        protected final int MAX=100;
        protected Ruta rute[]=new Ruta[MAX];
        protected int nrRute=0;
       
        @SuppressWarnings("rawtypes")
        public Stack btStack=new Stack();
       
        public void adaugaRuta(String p, String d, int distanta, boolean skip, boolean rutier, boolean feroviar, boolean fluvial)
        {
                if(nrRute<MAX)
                {
                        Ruta z=new Ruta(p, d, distanta, skip, rutier, feroviar, fluvial);
                        rute[nrRute]=z;
                        nrRute++;
                }
                else
                        System.out.println("Nu mai poate fi adaugata nicio ruta!");
        }
        public void setup()
        {
                adaugaRuta("Aradu Nou", "Timisoara", 48,false, true, true, false);
                adaugaRuta("Aradu Nou", "Ujszeged", 50, false, true, true, false);
                adaugaRuta("Aradu Nou", "Lipova", 18,false, true, true, false);
                adaugaRuta("Ujszeged", "Jimbolia", 25, false, true, true, false);
                adaugaRuta("Lipova", "Giarmata", 40,false, true, true, false);
                adaugaRuta("Timisoara", "Giarmata", 50, false, true, true, false);
                adaugaRuta("Timisoara", "Jimbolia", 18,false, true, true, false);
                adaugaRuta("Timisoara", "Foeni", 7, false, true, true, false);
                adaugaRuta("Timisoara", "Jebel", 48,false, true, true, false);
                adaugaRuta("Aradu Nou", "Sacosu Turcesc", 50, false, true, true, false);
                adaugaRuta("Aradu Nou", "Recas", 18,false, true, true, false);
                adaugaRuta("Jimbolia", "Foeni", 7, false, true, true, false);
                adaugaRuta("Jimbolia", "Srpska Kruja", 48,false, true, true, false);
                adaugaRuta("Giarmata", "Ianova", 50, false, true, true, false);
                adaugaRuta("Srpska Kruja", "Zrenjanin", 18,false, true, true, false);
                adaugaRuta("Foeni", "Ciacova", 7, false, true, true, false);
                adaugaRuta("Ianova", "Recas", 15,false, true, true, false);
                adaugaRuta("Recas", "Topolovat", 50, false, true, true, false);
                adaugaRuta("Topolovat", "Lugoj", 10,false, true, true, false);
                adaugaRuta("Topolovat", "Buzias", 7, false, true, true, false);
                adaugaRuta("Topolovat", "Sacosu Turcesc", 10,false, true, true, false);
                adaugaRuta("Lugoj", "Caransebes", 50, false, true, true, false);
                adaugaRuta("Lugoj", "Buzias", 10,false, true, true, false);
                adaugaRuta("Buzias", "Sacosu Turcesc", 7, false, true, true, false);
                adaugaRuta("Sacosu Turcesc", "Jebel", 10,false, true, true, false);
                adaugaRuta("Jebel", "Deta", 7, false, true, true, false);
                adaugaRuta("Jebel", "Ciacova", 50, false, true, true, false);
                adaugaRuta("Ciacova", "Deta", 18,false, true, true, false);
                adaugaRuta("Deta", "Vrsac", 7, false, true, true, false);
                adaugaRuta("Zrenjanin", "Vrsac", 15,false, true, true, false);
                adaugaRuta("Vrsac", "Panceva", 50, false, true, true, false);
                adaugaRuta("Vrsac", "Naidas", 10,false, true, true, false);
                adaugaRuta("Jebel", "Resita", 7, false, true, true, false);
                adaugaRuta("Resita", "Anina", 10,false, true, true, false);
                adaugaRuta("Resita", "Caransebes", 50, false, true, true, false);
                adaugaRuta("Caransebes", "Mehadia", 10,false, true, true, false);
                adaugaRuta("Resita", "Anina", 7, false, true, true, false);
                adaugaRuta("Anina", "Naidas", 10,false, true, true, false);
                adaugaRuta("Anina", "Mehadia", 7, false, true, true, false);
                adaugaRuta("Mehadia", "Orsova", 15,false, true, true, false);
                adaugaRuta("Naidas", "Moldova Noua", 50, false, true, true, false);
                adaugaRuta("Moldova Noua", "Orsova", 10,false, true, true, false);
               
        }
        public int match(String plecare, String destinatie)
        {
                int dist=0;
                for(int i=0;i< nrRute; i++)
                {
                        if(rute[i].getLocalitatePlecare().equals(destinatie)&& rute[i].isSkip()==false)
                        {
                                dist=rute[i].getDistanta();
                                rute[i].setSkip(false);
                        }
                }
                return dist;
        }
        public Ruta find(String from)
        {
                Ruta r=null;
                for(int i=0; i<nrRute; i++)
                {
                        if(rute[i].getLocalitatePlecare().equals(from) && rute[i].isSkip()==false)
                        {
                                r=new Ruta(from, rute[i].getLocalitateSosire(), rute[i].getDistanta(), rute[i].isSkip(), rute[i].isFluvial(), rute[i].isRutier(), rute[i].isFeroviar());
                                rute[i].setSkip(true);
                        }
                }
                return r;
        }
        @SuppressWarnings("unchecked")
        public void calculRuta(String from, String to)
        {
                int dist=0;
                Ruta z;
                if(match(from, to)>0)
                {
                        btStack.push(match(from, to));
                        dist=(Integer)btStack.pop();
                        return ;
                       
                }
                else
                {
                       
                        z=find(from);
                        if(z!=null)
                        {
                                //distanta dintre nodul curent si noul nod expandat
                                System.out.println("Pana in "+z.getLocalitateSosire()+" am parcurs "+z.getDistanta());
                                btStack.push(z.getDistanta());
                                //daca am ajuns la solutie
                                if( z.getLocalitateSosire().equals(to))
                                {
                                        System.out.println("Am ajuns la destinatie.");
                                        while(!btStack.empty())
                                {
                                                dist=dist+ (Integer)btStack.pop();        
                                }
                                        System.out.println("In total am de parcurs " + dist + " km.");
                                        return;
                                }
                                else
                                        calculRuta(z.getLocalitateSosire(), to);
                        }
                       
                }
               
               
        }
       
       
}

